import Vue from 'vue'
import Vuex from 'vuex'
import Axios from 'admin/config/axios'

Vue.use(Vuex);

const state = {
	permission : {},
    route :[],
    menu : {
        moduleActive: '',
        itemActive: '',
        display:{
            config : false,
            person : false,
            wechat : false,
        },
        openName:'',
    },
    admin :{},
}

const mutations = {
    //权限
	PER(state,data){
        state.permission = data;
	},
    //当前登录用户信息
    ADMIN(state,data){
        state.admin = data;
    },
    //路由TAG
    ROUTE(state,data){
        state.route = data;
    },
    //菜单模块选中
    MENU_MODULE_ACTIVE(state,moduleActive){
        state.menu.moduleActive = moduleActive;
    },
    //菜单项目选中
    MENU_ITEM_ACTIVE(state,itemActive){
        state.menu.itemActive = itemActive;
    },
    //菜单显示项目
    MENU_DISPLAY(state,display){
        for(var i in state.menu.display){
            state.menu.display[i] = false;
        }
        state.menu.display[display] = true;
    },
    //菜单打开的名称列表
    MENU_OPENNAME(state,openName){
        state.menu.openName = openName;
    },

}

const actions = {
    //获取用户权限列表
	SET_PER({ commit}){
        Axios.AdminInfo.Permission()
        .then(res=>{
            if(!res) return false;
            var data = res.data;
            commit('PER',data);
        })
	},
    //获取用户全局调用信息
    GET_ADMIN_COMMON({ commit}){
        Axios.AdminInfo.Common()
        .then(res=>{
            if(!res) return false;
            var data = res.data;
            commit('ADMIN',data);
        })
    },
    //改变路由
    CHANGE_ROUTE({commit,state},obj){
        //进入某个路由
        if(obj.action==1){
            var existFlog = false,index = 0,data = state.route;
            for(var i in data){
                if(data[i].route==obj.route){
                    existFlog = true ; index = i;
                }
                data[i].color = 'default';
            }
            if (existFlog) {
                data[index].color = 'primary';
            }else{
                data.push({name:obj.name,route:obj.route,color:'primary',closable:obj.closable});
            }
            var path = document.location.href.split('?')[0].split('#/'),
                pathArr = path.length==1 ? new Array() : path[1].split('/'),
                module  = pathArr[0] !=undefined ? pathArr[0] : '',
                item    = pathArr[1]!=undefined ? pathArr[1] : '',
                child   = pathArr[2]!=undefined ? pathArr[2] : '';
            //设置当前模块菜单栏的选中或打开的菜单
            commit('MENU_MODULE_ACTIVE',module);

            var activeitem = '/'+module+'/'+item;
            var action = {'update':true,'view':true};
            //不在操作页面表明当前路由是三级
            if(child!='' && !action[child]){
                activeitem +=  '/'+child;
            }
            //设置选中的项目
            commit('MENU_ITEM_ACTIVE',activeitem);
            //显示的模块
            commit('MENU_DISPLAY',module);
            //打开的模块
            commit('MENU_OPENNAME','/'+module+'/'+item);

            commit('ROUTE',data);
        }
        //退出路由
        if(obj.action==2){
            var data = state.route,activeFlog=false;
            for(var i in data){
                if(data[i].route==obj.route){
                    data.splice(i,1);
                }
            }
            for(var i in data){
                if(data[i].color=='primary'){
                    activeFlog = true;
                }
            }
            //删除的TAG是打开的路由
            if (!activeFlog) {
                data[data.length-1].color = 'primary';
            }
            commit('ROUTE',data);
        }
        //关闭除了传进的路由、首页路由
        if(obj.action==3){
            var data = state.route;
            for(var i =0;i<data.length;i++){
                if(data[i].route!='/' && data[i].color=='default'){
                    data.splice(i,1);
                    i--;
                }
            }
            commit('ROUTE',data);
        }
        //关闭除了首页的路由
        if(obj.action==4){
            var data = state.route;
            for(var i =0;i<data.length;i++){
                if(data[i].route!='/'){
                    data.splice(i,1);
                    i--;
                }else{
                    data[i].color = 'primary';
                }
            }
            commit('ROUTE',data);
        }
    },
    //菜单改变
    CHANGE_MENU({commit,state},obj){
        //菜单模块选中
        if(obj.action==1){
           commit('MENU_MODULE_ACTIVE',obj.moduleActive);
        }
        //菜单项目选中
        if(obj.action==2){
            commit('MENU_ITEM_ACTIVE',obj.itemActive);
        }
        //菜单模块显示
        if(obj.action==3){
            commit('MENU_DISPLAY',obj.module);
        }
        //打开的项目
        if(obj.action==4){
            commit('MENU_OPENNAME',obj.openName);
        }
    }
}

const getters= {
    //权限
    per:function(state){
        return state.permission;
    },
    //路由
    route:function(state){
        return state.route;
    },
    //菜单
    menu:function(state){
        return state.menu;  
    },
    //用户
    admin:function(state){
        return state.admin;
    }
}

const store = new Vuex.Store({
  	state : state,
  	mutations : mutations,
  	actions : actions,
  	getters : getters,
})
export default store;